package com.wanyang.aliyun.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.wanyang.aliyun.configure.AccessProperties;
import com.wanyang.dto.AliyunSmsDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AliyunSmsService {

    @Autowired
    private AccessProperties accessProperties;

    public void sendSms(AliyunSmsDTO smsDTO) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                accessProperties.getAccessKeyId(), accessProperties.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", smsDTO.getMobile());
        request.putQueryParameter("SignName", smsDTO.getSignName());
        request.putQueryParameter("TemplateCode", smsDTO.getTemplateCode());
        request.putQueryParameter("TemplateParam", smsDTO.getTemplateParam());

        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }


}
